Skip to content

test: add third-party script error handling and duplicate prevention coverage - #888

Open
JemimahEkong wants to merge 1 commit into
Iris-IV:mainfrom
JemimahEkong:test/third-party-script-loading
Open

test: add third-party script error handling and duplicate prevention coverage#888
JemimahEkong wants to merge 1 commit into
Iris-IV:mainfrom
JemimahEkong:test/third-party-script-loading

Conversation

@JemimahEkong

Copy link
Copy Markdown
Contributor

PR: test(third-party): add script error handling and duplicate prevention coverage

This PR improves third-party script reliability by adding coverage for script load failures and preventing duplicate script injection scenarios.

Problem

The third-party script loading module lacked tests for two important edge cases:

  1. Handling failed third-party script loads through error events.
  2. Preventing duplicate script entries from creating multiple DOM script nodes.

What changed

src/lib/thirdParty.ts

  • Added optional onError?: () => void support to ThirdPartyScript so consumers can provide script-specific failure handlers.

  • Added duplicate script protection in getThirdPartyScripts():

    • Scripts with duplicate id values are filtered out.
    • Prevents accidental duplicate script injection at the configuration layer.
  • Exported handleScriptError(script) helper:

    • Safely invokes a configured error callback when a script fails to load.

src/components/ThirdPartyScripts.tsx

  • Updated script rendering logic to:

    • Extract onError from each script configuration.
    • Forward it to the Next.js <Script onError={...}> handler.

Test coverage

Added 2 new tests in:

src/__tests__/lib/thirdParty.test.ts

Covered scenarios:

Script load error handling

  • Confirms default scripts do not define error handlers.
  • Verifies custom onError callbacks are executed through handleScriptError.

Duplicate script prevention

  • Confirms returned scripts always contain unique IDs.
  • Ensures duplicate configurations cannot result in duplicate script injection.

Validation

  • ✅ All 12 tests passing

    • 10 existing tests
    • 2 new tests
  • ✅ No regressions introduced

Closes #844

@drips-wave

drips-wave Bot commented Jul 30, 2026

Copy link
Copy Markdown

@JemimahEkong Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@sshdopey

Copy link
Copy Markdown
Contributor

Auto-review failed (API error). Leaving PR for human review.

1 similar comment
@sshdopey

sshdopey commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Auto-review failed (API error). Leaving PR for human review.

…coverage

- Add onError callback to ThirdPartyScript interface
- Add dedup-by-id guard in getThirdPartyScripts()
- Wire onError prop in ThirdPartyScripts component
- Add handleScriptError() helper
- Add tests for error event handling and duplicate script IDs
@JemimahEkong
JemimahEkong force-pushed the test/third-party-script-loading branch from 39b474f to fd7e331 Compare August 3, 2026 20:24
@sshdopey

sshdopey commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Auto-review failed (API error). Leaving PR for human review.

@davidmaronio davidmaronio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good additions overall: the optional onError on ThirdPartyScript is a clean extension point, wiring it through the Script component in ThirdPartyScripts.tsx is the right place, and the dedup-by-id guard in getThirdPartyScripts is cheap insurance with a test asserting id uniqueness.

a few things to tighten up:

  1. src/lib/thirdParty.ts:167 handleScriptError is exported but never used by production code; ThirdPartyScripts passes onError straight to Script. either route the component through handleScriptError (and give it a default, e.g. a dev console.warn when no onError is configured, which is what the issue is really about: failures are currently silent) or drop the helper.
  2. src/tests/lib/thirdParty.test.ts:137 the "handles script load errors" test only exercises handleScriptError with a hand-built object, it never verifies that a script error event actually reaches the handler. a component-level test that fires the error on the rendered Script mock would cover the real path.
  3. typecheck, unit tests, build, and bundle check are red. some of that is the stale base fixed by #875, but since this PR changes prod files please rebase onto current main and confirm all of those go green rather than assuming.

@davidmaronio

Copy link
Copy Markdown
Contributor

no changes since the review. handleScriptError is still unused by production code, the error-path test still never fires a real script error event, and typecheck/build/unit tests are still red. please address the three points and rebase onto current main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Testing] Add unit tests for thirdParty.ts script-loading guard

3 participants